<?php
namespace app\index\controller;

use think\Controller;
use think\Db;
use think\Request;
use think\Session;
use app\index\model\Nav;
use app\index\model\User;
use app\index\model\Consult;

class Data extends Controller
{
	public function __construct(){
		
    }

	/**
     * 一级导航
     * @access public
     * @param site 位置（上中下）
     */
	public function NavCat()
	{
		$m_nav = new Nav();
		$site = input('post.site');
		$where = '';
		if ($site) {
			$where['site'] = $site;
		}
		$nav_cat = $m_nav->get_NavCatList($where);
		return json($nav_cat);
	}

	/**
     * 导航列表
     * @access public
     * @param site 位置（上中下）
     */
	public function NavCatList()
	{
		$m_nav = new Nav();
		$site = input('post.site');
		$where = '';
		if ($site) {
			$where['site'] = $site;
		}
		$navlist = $m_nav->get_NavCatList($where);
		foreach ($navlist as $key => $value) {
			$navlist[$key]['list'] = $m_nav->getNavList(array('cat_id'=>$value['id']));
		}
		return json($navlist);
	}

	public function NavLabel()
	{
		$m_nav = new Nav();
		$site = input();
	}

	//二级导航列表
	public function NavList()
	{
		$m_nav = new Nav();
		$cat_id = input('post.cat_id');
		$where = array();
		if ($cat_id) {
			$where['cat_id'] = $cat_id;
		}else{
			$error = [
				'msg' => '请求接口参数错误',
				'result' => 400
			];
			return json($error);
		}
		$data = $m_nav->getNavList($where);
		return json($data);
	}

	/**
     * 友情链接列表
     * @access public
     */
	public function Like()
	{
		$m_nav = new Nav();
		$like = $m_nav->getLikeList();
		return json($like);
	}

	/**
     * 首页轮播文章
     * @access public
     */
	public function index_hot_new()
	{
		$new = new consult();
		$user = new User();
		$hot_new = $new->getHomeNew();
		foreach ($hot_new as $key => $value) {
			$member = $user->get_MemberOne($value['user_id']);
			$hot_new[$key]['member_name'] = $member['member_name'];
			$hot_new[$key]['head_pic'] = $member['head_pic'];
		}
		return json($hot_new);
	}

	/**
     * 首页推荐文章
     * @access public
     */
	public function hot_new()
	{
		$new = new Consult();
		$cat = input('post.cat');
		$where = '';
		if ($cat) {
			$where['nav_id'] = $cat;
		}else{
			$where['nav_id'] = 1;
		}
		$hot_new = $new->getHotNew($where);
		return json($hot_new);
	}

	//分类下面所有有效新闻
	public function newlist()
	{
		$new = new Consult();
		$cat = input('post.cat');
		if ($cat) {
			$where['nav_id'] = $cat;
		}else{
			$where['nav_id'] = 1;
		}
		$data = $new->getNew($where);
		foreach ($data as $key => $value) {
			$label = explode(",",$value['label']);
        	$i=0;
	        foreach ($label as $k => $v) {
	            if ($v !='') {
	                $label_v[$i] = $new->getLabelOne($v);
	                $i++;
	            }
	        }
	        $data[$key]['label_v'] = $label_v;
		}
		return json($data);
	}

	//首页推荐作者
	public function hot_gather()
	{
		$user = new User();
		$gather = $user->get_Gather();
		return json($gather);
	}

	//热门资讯
	public function hot_message()
	{
		$new = new Consult();
		$cat_id = input('post.cat_id');
		$where = '';
		if ($cat_id) {
			$where['nav_id'] = $cat_id;
		}else{
			$error = [
				'msg' => '请求接口参数错误',
				'result' => 400
			];
			return json($error);
		}
		$hot_new = $new->getHomeHotNew($where);
		return json($hot_new);
	}

	//标签
	public function label()
	{
		$new = new Consult();
		$cat = input('cat_id');
		if ($cat) {
			$where['cat_id'] = $cat;
		}else{
			$where = '';
		}
		$label = $new->getLabel($where);
		return json($label);
	}

	//内容详情页面
	public function content()
	{
		$new = new Consult();
		$user = new User();
		$id = input('param.id');
		if ($id) {
			$data = $new->getContent($id);
			$label = explode(',',$data['label']);
			$i = 0;
			foreach ($label as $key => $value) {
				if ($value !='') {
	                $label_v[$i] = $new->getLabelOne($value);
	                $i++;
	            }
			}
			$data['label_v'] = $label_v;
			$data['member'] = $user->getMemberInfo($data['user_id']);
			return json($data);
		}else{
			$data = [
				'msg'=>'接口数据错误!',
				'result'=>400
			];
			return json($data);
		}
	}

	//通过文章查找用户信息
	public function contentUser()
	{
		$user = new User();
		$id = input('param.id');

	}

	//评论接口
	public function comments()
	{
		$new = new Consult();
		$id = input('param.id');
		$token = input('param.key');
		if ($token != Session::get('token')) {
			$data = [
				'msg'=>'密钥不对！',
				'result'=>400
			];
			return json($data);
		}
		if ($id == false) {
			$data = [
				'msg' => '接口参数不对！',
				'result' =>	400
			];
			return json($data);
		}
		$data = $new->getCollect($id);
	}

	//收藏接口
	public function collect()
	{
		$user = new User();
		$token = input('param.key');
		$userId = Session::get('userId');
		if ($token != Session::get('token')) {
			$data = [
				'msg'=>'密钥不对！',
				'result'=>400
			];
			return json($data);
		}
		$id = input('param.id');
		if ($id == false) {
			$data = [
				'msg' => '接口参数不对！',
				'result' =>	400
			];
			return json($data);
		}
		$data = $user->getCollect($id,$userId);
		return json($data);
	}

	//关注接口
	public function attention()
	{
		$user = new User();
		$token = input('param.key');
		$userId = input('param.userId');
		$userIds = Session::get('userId');
		if ($userId==$userIds) {
			$data = [
				'msg'=>'自己不能关注自己',
				'result'=>400
			];
			return json($data);
		}
		if ($token != Session::get('token')) {
			$data = [
				'msg'=>'密钥不对！',
				'result'=>400
			];
			return json($data);
		}
		$id = input('param.id');
		if ($id == false) {
			$data = [
				'msg' => '接口参数不对！',
				'result' =>	400
			];
			return json($data);
		}
		$data = $user->getAttention($userIds,$userId);
		return json($data);
	}

	//用户回复
	public function reply()
	{
		$new = new Consult();
	}

	//获取专栏列表
	public function author()
	{
		$user = new User();
		$order = input('param.order');
		if ($order == false) {
			$order = '';
		}
		$data = $user->getAuthor($order);
		return json($data);
	}

	//获取单个用户信息
	public function authorInfo()
	{
		$user = new User();
		$userId = input('param.userId');
		if ($userId == false) {
			$data = [
				'msg' => '接口参数不对！',
				'result' =>	400
			];
			return json($data);
		}
		$data = $user->getAuthorInfo($userId);
		return json($data);
	}

	//用户文章列表
	public function ConsultList()
	{
		$user = new User();
		$userId = input('param.userId');
		if ($userId == false) {
			$data = [
				'msg' => '接口参数不对！',
				'result' =>	400
			];
			return json($data);
		}
		$data = $user->getUserConsult($userId);
		return json($data);
	}

	//用户评论列表
	public function CommentsList()
	{
		$user = new User();
		$userId = input('param.userId');
		if ($userId == false) {
			$data = [
				'msg' => '接口参数不对！',
				'result' =>	400
			];
			return json($data);
		}
		$data = $user->getUserComments($userId);
		return json($data);
	}

	//搜索文章
	public function searchArticle()
	{
		$new = new Consult();
		$search = input('param.search');
		$data = $new->getSearchArticle($search);
		return json($data);
	}

	//搜索视频
	public function searchVideo()
	{
		$new = new Consult();
		$search = input('param.search');
		$data = $new->getSearchVideo($search);
		return json($data);
	}

	//搜索作者
	public function searchWriter()
	{
		$new = new Consult();
		$search = input('param.search');
		$data = $new->getSearchWriter($search);
		return json($data);
	}

	//按照标签找文章
	public function searchLabel()
	{
		$new = new Consult();
		$id = input('param.id');
		$data = $new->getLabelArticle($id);
		return json($data);
	}

	//个人中心
	public function EditMember()
	{
		
	}

	//个人中心收藏
	public function MemberCollect()
	{
		$new = new Consult();
		$userId = input('param.userId');
		if ($userId == false) {
			$data = [
				'msg' => '接口参数不对！',
				'result' =>	400
			];
			return json($data);
		}
		$data = $new->getUserCollect($userId);
		return json($data);
	}

	//个人中心评论接口
	public function MemberComments()
	{
		$new = new Consult();
		$userId = input('param.userId');
		if ($userId == false) {
			$data = [
				'msg' => '接口参数不对！',
				'result' =>	400
			];
			return json($data);
		}
		$data = $new->getUserComments($userId);
		return json($data);
	}

	//个人中心我关注的人
	public function MemberAttention($value='')
	{
		$new = new Consult();
		$userId = input('param.userId');
		if ($userId == false) {
			$data = [
				'msg' => '接口参数不对！',
				'result' =>	400
			];
			return json($data);
		}
		$data = $new->getUserAttention($userId);
		return json($data);
	}
}